#include <iostream>

#include "gflags/gflags.h"

#include "uxpng_all.h"


DEFINE_string(in_img1, "", "the img1 filename");
DEFINE_string(in_img2, "", "the img2 filename");
DEFINE_string(out_img, "", "the img filename where to write out");
DEFINE_string(img, "", "the img you want to read");

DEFINE_int32(option, 0, "option num, from 1 ~ 3");

void MergePng(const char * in1, const char * in2, const char * out) {
  ux_png::PngData img1;
  ux_png::PngData img2;
  ux_png::ReadAndCheckPNGFromImgFile(in1, &img1);
  ux_png::ReadAndCheckPNGFromImgFile(in2, &img2);
  img1.LeftPut(img2);
  ux_png::WritePngDataToImgFile(out, &img1);
}

void ReadPngInfo(const char * img_path) {
  ux_png::PngData img;
  ux_png::ReadAndCheckPNGFromImgFile(img_path, &img);
}

void Rgb2Gray(const char * img_path, const char * out_img_path) {
  ux_png::PngData img;
  ux_png::ReadAndCheckPNGFromImgFile(img_path, &img);
  std::cout << "before rgb2gray: "<< ux_png::ColorType2String(img.ColorType()) << "\n";
  img.Rgb2Gray();
  ux_png::WritePngDataToImgFile(out_img_path, &img);
  std::cout << "after rgb2gray: " << ux_png::ColorType2String(img.ColorType()) << "\n";
}

int main(int argc, char ** argv) {
  gflags::SetUsageMessage("Usage Message:\n"
                          "option 1: MergePng() in_img1, in_img2, out_img\n"
                          "option 2: ReadPngInfo() img\n"
                          "option 3: Rgb2Gray() img out_img\n");
  gflags::ParseCommandLineFlags(&argc, &argv, true);
  switch (FLAGS_option) {
    case 1:
      MergePng(FLAGS_in_img1.c_str(), FLAGS_in_img2.c_str(), FLAGS_out_img.c_str());
      break;
    case 2:
      ReadPngInfo(FLAGS_img.c_str());
      break;
    case 3:
      Rgb2Gray(FLAGS_img.c_str(), FLAGS_out_img.c_str());
      break;
    default: std::cout << gflags::ProgramUsage() << "\n";
  }
  gflags::ShutDownCommandLineFlags();
  return 0;
}
